package com.hcss.picture.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hcss.common.base.BaseClass;
import com.hcss.picture.constant.PictureConstant;
import com.hcss.util.FileUtil;
import org.apache.commons.collections.CollectionUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.hcss.paging.service.SchemaPagingByExample;
import com.hcss.util.EmptyUtil;
import com.hcss.picture.dao.PictureMapper;
import com.hcss.picture.model.Picture;
import com.hcss.picture.model.PictureExample;
import com.hcss.picture.model.par.PicturePar;
import com.hcss.picture.service.PictureService;

/**
 * @描述: 图片的model层-服务的实现类
 * @作者: guoxia
 * @创建日期: 2023-03-01 11:55:22
 */
@Service("pictureService")
public class PictureServiceImpl implements PictureService {

	@Autowired
	private PictureMapper pictureMapper;

	@Autowired
	RedisTemplate redisTemplate;

	@Override
	public PageInfo< Picture > queryPagingData( PicturePar par ) throws Exception {
		PictureExample example=(PictureExample) SchemaPagingByExample.getBaseExample( PictureExample.class,par );
		PageHelper.startPage( par.getPage( ),par.getPerpage( ) );
		List< Picture > list=pictureMapper.selectByExample( example );
		return new PageInfo<>( list );
	}

	@Override
	public List< Picture > queryAllData( PicturePar par ) throws Exception {
		PictureExample example=(PictureExample) SchemaPagingByExample.getBaseExample( PictureExample.class,par );
		example.setOrderByClause( "PICTURE_TIME" );
		return pictureMapper.selectByExample( example );
	}

	@Override
	public void insertModel( Picture model ) {
		//生成一个随机的ID
		String ID=BaseClass.getUUIDWithoutConnector( );
		model.setId( ID );
		//生成操作人
		model.setOptiorName( redisTemplate.opsForValue().get( "operator" ).toString() );
		//生成创建时间
		model.setPictureTime( new Date( ) );
		model.setIfDelete( PictureConstant.IF_DLETE_NO );
		pictureMapper.insertSelective( model );
	}

	@Override
	public void updateModel( Picture model ) {
		PictureExample example=new PictureExample( );
		// TODO 补充更新条件
		example.createCriteria().andIdEqualTo( model.getId() );
		try {
			String oldPictureName=redisTemplate.opsForValue( ).get( model.getId( ) ).toString();
			//修改文件中的名字
			File file=new File(PictureConstant.PICTURE_ADDRESS_HOSTORICAL+FileUtil.dirSperator+oldPictureName );
			example.createCriteria().andIdEqualTo( model.getId( ) );
			pictureMapper.updateByExampleSelective( model,example );
			file.renameTo( new File( PictureConstant.PICTURE_ADDRESS_HOSTORICAL+FileUtil.dirSperator+model.getPictureName()  ));
		} catch(Exception e) {
			e.printStackTrace( );
		}
	}

	@Override
	public void deleteByIds( List< String > ids ) {
		if(!EmptyUtil.isEmpty( ids )) {
			Picture more=new Picture( );
			more.setIfDelete( PictureConstant.IF_DLETE_YES );
			PictureExample example=new PictureExample( );
			example.createCriteria( ).andIdIn( ids );
			pictureMapper.updateByExampleSelective( more,example );
		}
	}

	@Override
	public Picture selectByPK( String id ) {
		if(EmptyUtil.isHaveEmpty( id )) return null;

		PictureExample example=new PictureExample( );
		example.createCriteria( ).andIdEqualTo( id );
		List< Picture > list=pictureMapper.selectByExample( example );
		return CollectionUtils.isEmpty( list )? null : list.get( 0 );
	}

	@Override
	public List< Picture > queryNames( PicturePar par ) throws Exception {
		List< Picture > pictures=queryAllData( par );
		return pictures;
	}

	@Override
	public List< String > selectNamesByIds( String[] ids ) {
		List< String > list=Arrays.asList( ids );
		PictureExample example=new PictureExample( );
		example.createCriteria().andIdIn(list);
		List< Picture > pictures=pictureMapper.selectByExample( example );
		List< String > pictureNames=pictures.stream( ).map( picture->picture.getPictureName( ) ).collect( Collectors.toList( ) );
		return pictureNames;
	}
}
